package com.ft.adbix.utils;

import com.ft.adbix.core.ADBixSdk;
import com.ft.adbix.core.response.Ad;
import com.ft.adbix.core.service.DelayedRequestService;
import com.ft.adbix.utils.data.common.MacroConst;
import com.ft.adbix.utils.http.HttpHelper;
import com.ft.adbix.utils.http.listener.HttpListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdTrackerReporter {

    private final Ad adData;
    private final Map<String, String> macros = new HashMap<>();

    public AdTrackerReporter(Ad adData) {
        this.adData = adData;
        macros.put(MacroConst.BID_PRICE, String.valueOf(adData.getBidPrice()));
    }

    public void markDown(float downX,float downY,float absDownX, float absDownY){
        macros.put(MacroConst.DOWN_X,String.valueOf(downX));
        macros.put(MacroConst.DOWN_Y,String.valueOf(downY));
        macros.put(MacroConst.ABS_DOWN_X,String.valueOf(absDownX));
        macros.put(MacroConst.ABS_DOWN_Y,String.valueOf(absDownY));
    }

    public void markUp(float upX,float upY,float absUpX, float absUpY){
        macros.put(MacroConst.UP_X,String.valueOf(upX));
        macros.put(MacroConst.UP_Y,String.valueOf(upY));
        macros.put(MacroConst.ABS_UP_X,String.valueOf(absUpX));
        macros.put(MacroConst.ABS_UP_Y,String.valueOf(absUpY));
    }

    public String getMarkInfo(){
        StringBuilder sbInfo = new StringBuilder();
        for (String s : macros.keySet()) {
            sbInfo.append(s).append(":").append(macros.get(s)).append("\n");
        }
        return sbInfo.toString();
    }

    public void markAdSize(int width,int height){
        macros.put(MacroConst.AD_WIDTH,String.valueOf(width));
        macros.put(MacroConst.AD_HEIGHT,String.valueOf(height));
    }

    public void markPlayDuration(long duration){
        macros.put(MacroConst.PLAY_DURATION, String.valueOf(duration/1000));
        macros.put(MacroConst.PLAY_MILLI_DURATION, String.valueOf(duration));
    }

    public void markPlayFinish(boolean isFinish){
        macros.put(MacroConst.PLAY_FINISH, String.valueOf(isFinish?1:0));
    }

    private void markCurrentTime(){
        macros.put(MacroConst.EVENT_TIME, String.valueOf(System.currentTimeMillis()/1000));
        macros.put(MacroConst.EVENT_MILLI_TIME, String.valueOf(System.currentTimeMillis()));
    }

    public void reportDisplay(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getDisplay());
    }

    public void reportClick(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getClick());
    }

    public void reportTryDeeplink(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getTryDeeplink());
    }

    public void reportDeeplink(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        reportInBackground(adData.getTracker().getDeeplink(),10);
//        report(adData.getTracker().getDeeplink());
    }

    public void reportWechatOpen(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getWechatOpen());
    }

    public void reportAppStoreOpen(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getAppStoreOpen());
    }

    public void reportDownloadStart(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getDownloadStart());
    }

    public void reportDownloadEnd(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getDownloadEnd());
    }

    public void reportInstallStart(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getInstallStart());
    }

    public void reportInstallEnd(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getDownloadEnd());
    }

    public void reportOpen(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        reportInBackground(adData.getTracker().getOpen(),10);
//        report(adData.getTracker().getOpen());
    }

    public void reportVideoLoaded(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoLoaded());
    }

    public void reportVideoError(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoError());
    }

    public void reportVideoStart(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoStart());
    }

    public void reportVideoQuarter(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoQuarter());
    }

    public void reportVideoMiddle(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoMiddle());
    }

    public void reportVideoThirdQuarter(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoThirdQuarter());
    }

    public void reportVideoEnd(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoEnd());
    }

    public void reportVideoPause(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoPause());
    }

    public void reportVideoResume(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoResume());
    }

    public void reportVideoSkip(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoSkip());
    }

    public void reportVideoMute(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoMute());
    }

    public void reportVideoUnmute(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoUnmute());
    }

    public void reportVideoReplay(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoReplay());
    }

    public void reportVideoClose(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoClose());
    }

    public void reportVideoFullScreen(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoFullScreen());
    }

    public void reportVideoExitFullScreen(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getVideoExitFullScreen());
    }

    public void reportRewardSuccess(){
        if (adData==null||adData.getTracker()==null){
            return;
        }
        report(adData.getTracker().getRewardSuccess());
    }

    private void report(String[] trackerUrls) {
        markCurrentTime();
        if (trackerUrls != null) {
            for (String s : trackerUrls) {
                request(replace(s));
            }
        }
    }

    private void reportInBackground(String[] trackerUrls, int delaySeconds) {
        markCurrentTime();
        if (trackerUrls != null) {
            for (String s : trackerUrls) {
//                request(replace(s));
                DelayedRequestService.scheduleRequest(ADBixSdk.getInstance().getContext(), replace(s), delaySeconds);
            }
        }
    }

    public String replace(String sourceUrl) {
        for (String s : macros.keySet()) {
            if (!sourceUrl.contains(s)){
                continue;
            }
            String targetValue = macros.get(s);
            if (targetValue != null) {
                sourceUrl = sourceUrl.replaceAll(s, targetValue);
            }
        }
        return sourceUrl;
    }

    private static void request(String url) {
        HttpHelper.post(url, null, new HttpListener() {
            @Override
            public void onRequestStart() {

            }

            @Override
            public void onRequestFailed(int code, String error) {
                AdLogUtils.d("tracker failed code = "+code+" -- msg = "+error);
            }

            @Override
            public void onRequestSuccess(String result) {
                AdLogUtils.d("tracker success url = "+url);
            }

            @Override
            public void onRequestSuccess(String result, Map<String, List<String>> responseHeader) {

            }
        });
    }

}
